﻿using PIMS.DBModel;
using PIMS.Interface;
using PIMS.WorkPlugin.ORM;
using PIMS.Utils;
using PIMS.WorkPlugin.Util;
using PIMS.XmlModel;
using PIMS_Interface.Interface;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using PIMS_Interface.DBModel;

namespace PIMS.WorkPlugin
{
    public partial class FormEditBack : Form, IFormEdit
    {
        PIMSConfigX pimsConfig = XmlHelper.XmlFileToObject<PIMSConfigX>(AppDomain.CurrentDomain.BaseDirectory + "config\\PIMSConfig.xml");
        //月份对照字典
        Dictionary<string, string> monthDict = new Dictionary<string, string>();
        private IDebugMsg debug = null;
        private ILog4net log = null;
        private orderformcurrent order = null;
        private int lineNO;
        private bool bresult;
        public orderformcurrent RetValue
        {
            get
            {
                return this.order;
            }
            set
            {
                this.order = value;
            }
        }

        public FormEditBack()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 注：反射加载时会自动调用一次
        /// </summary>
        public void Init(IDebugMsg debugMsg, ILog4net log4net)
        {
            this.debug = debugMsg;
            this.log = log4net;

            //try
            //{
            //    InitComboBoxByString(this.cb包装设备, pimsConfig.GetAppValueByKey("deviceName"));
            //    DataDictTools.InitComboBoxByDataDic(this.cb品名, "品名");
            //    DataDictTools.InitComboBoxByDataDic(this.cb牌号, "牌号");
            //    DataDictTools.InitComboBoxByDataDic(this.cb等级, "等级");
            //}
            //catch (Exception ex)
            //{
            //    this.debug.DebugMsg(ex.Message, InfoLevel.error);
            //    this.log.Error("", ex);
            //}
        }

        private void FormEdit_Load(object sender, EventArgs e)
        {
            monthDict.Add("01", "LA");
            monthDict.Add("02", "LM");
            monthDict.Add("03", "LB");
            monthDict.Add("04", "LE");
            monthDict.Add("05", "LC");
            monthDict.Add("06", "LH");
            monthDict.Add("07", "LD");
            monthDict.Add("08", "LF");
            monthDict.Add("09", "LP");
            monthDict.Add("10", "AL");
            monthDict.Add("11", "AA");
            monthDict.Add("12", "AM");
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.err.Clear();

            if (String.IsNullOrEmpty(this.txtFactory.Text))
            {
                this.err.SetError(this.txtFactory, "禁止为空！");
                return;
            }
            if (String.IsNullOrEmpty(this.txtProductLine.Text))
            {
                this.err.SetError(this.txtProductLine, "禁止为空！");
                return;
            }
            //if (this.txtProductLine.Text.Length != 7)
            //{
            //    this.err.SetError(this.txtProductLine, "字符串长度异常！");
            //    return;
            //}
            //if (!IsInt(this.txtProductLine.Text.Substring(5)))
            //{
            //    this.err.SetError(this.txtProductLine, "格式异常！");
            //    return;
            //}
            if (String.IsNullOrEmpty(this.txtWorkNum.Text))
            {
                this.err.SetError(this.txtWorkNum, "禁止为空！");
                return;
            }
            if (String.IsNullOrEmpty(this.txtProject.Text))
            {
                this.err.SetError(this.txtProject, "禁止为空！");
                return;
            }
            if (String.IsNullOrEmpty(this.txtOrderNum.Text))
            {
                this.err.SetError(this.txtOrderNum, "禁止为空！");
                return;
            }
            if (String.IsNullOrEmpty(this.txtUnit.Text))
            {
                this.err.SetError(this.txtUnit, "禁止为空！");
                return;
            }
            if (String.IsNullOrEmpty(this.txtItemCode.Text))
            {
                this.err.SetError(this.txtItemCode, "禁止为空！");
                return;
            }
            if (String.IsNullOrEmpty(this.txtProduceName.Text))
            {
                this.err.SetError(this.txtProduceName, "禁止为空！");
                return;
            }
            if (String.IsNullOrEmpty(this.txtBrandNum.Text))
            {
                this.err.SetError(this.txtBrandNum, "禁止为空！");
                return;
            }
            if (String.IsNullOrEmpty(this.txtBatchRule.Text))
            {
                this.err.SetError(this.txtBatchRule, "禁止为空！");
                return;
            }
            if (!IsDouble(this.txtBatchRule.Text))
            {
                this.err.SetError(this.txtBatchRule, "请输入正浮点数！");
                return;
            }
            if (String.IsNullOrEmpty(this.cbxPalRule.Text))
            {
                this.err.SetError(this.cbxPalRule, "禁止为空！");
                return;
            }
            if (!IsDouble(this.cbxPalRule.Text))
            {
                this.err.SetError(this.cbxPalRule, "请输入正浮点数！");
                return;
            }

            if (string.IsNullOrEmpty(this.txtProductStage.Text))
            {
                this.txtProductStage.Text = "";
            }
            if (string.IsNullOrEmpty(this.txtQulityInfo.Text))
            {
                this.txtQulityInfo.Text = "";
            }
            if (string.IsNullOrEmpty(this.txtProductLevel.Text))
            {
                this.txtProductLevel.Text = "";
            }
            if (string.IsNullOrEmpty(this.txtPecoalNote.Text))
            {
                this.txtPecoalNote.Text = "";
            }
            if (string.IsNullOrEmpty(this.txtReclaimLogo.Text))
            {
                this.txtReclaimLogo.Text = "";
            }
            if (string.IsNullOrEmpty(this.txtPecialInfo.Text))
            {
                this.txtPecialInfo.Text = "";
            }

            //lineNO = int.Parse(this.txtProductLine.Text.Substring(5));
            //if (lineNO > 4)
            //{
            //    this.err.SetError(this.txtProductLine, "格式异常！");
            //    return;
            //}
            //string dt = DateTime.Now.ToString("yyyy-MM-dd");
            //string dtTemp = DateTime.Now.ToString("yy-MM-dd");
            //string[] dateItem = dtTemp.Split('-');
            //string batch = dateItem[0] + monthDict[dateItem[1]] + dateItem[2] + this.txtProductLine.Text;
            //string smallBatch = dateItem[2] + "01";

            orderformcurrent c = new orderformcurrent();
            //TODO 1030
            c.packager = this.txtFactory.Text;
            c.workOrder = this.txtFactory.Text;
            c.batchNo = this.txtFactory.Text;

            c.productName = this.txtFactory.Text;
            c.productMark = "中文"; //桶标语言
            //if (radioButtonEN.Checked) c.productMark = "英文";


            RetValue = c;
            this.Close();

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.order = null;
            this.Close();
        }

        private void InitComboBoxByString(ComboBox comboBox, string value)
        {
            comboBox.Items.Clear();
            string[] strs = value.Split('|');
            for (int i = 0; i < strs.Length; i++)
            {
                comboBox.Items.Add(strs[i]);
            }
        }

        /// <summary>
        /// 正则表达式验证中文字符
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static bool JustChinese(string text = "天下英雄abc")
        {
            for (int i = 0; i < text.Length; i++)
            {
                //判断汉字的Unicode编码范围
                if (Regex.IsMatch(text[i].ToString(), @"[\u4e00-\u9fbb]"))
                    return true;
            }
            return false;
        }

        //判断是否为正整数
        public static bool IsInt(string inString)
        {
            Regex regex = new Regex("^[0-9]*[1-9][0-9]*$");
            return regex.IsMatch(inString.Trim());
        }

        //判断字符串是否可以转化为正浮点数
        public static bool IsDouble(string inString)
        {
            Regex regex = new Regex("^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$");
            return regex.IsMatch(inString.Trim());
        }
    }
}
